<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        //多表联查
        //c1.*,c2.name AS parenName,CONCAT(c1.path,',',c1.id) AS sortPath FROM cate AS c1 LEFT JOIN cate AS c2 ON c1.pid=c2.id ORDER BY sortPath;
        
       $data = DB::table('cate AS c1')
        ->leftJoin('cate AS c2','c1.pid','=','c2.id')
        ->select('c1.*','c2.name AS parentName',DB::raw("CONCAT(c1.path,',',c1.id) AS sortPath"))
        ->where('c1.name','like','%'.$request ->input('keywords').'%')
        ->orderBy('sortPath')
        ->paginate($request -> input('num',10));

        foreach($data as $k=>$v)
        {   //获取path里','的个数
            //substr_repeat重复一个字符串
            $num = substr_count($v ->path,',');

            //利用','的个数去区分分类名
            $data[$k] -> name = str_repeat('|----', $num).$v ->name;
            if($data[$k] ->parentName =='')
            {
                $data[$k] ->parentName = '根分类';
            }
        }

        return view('admin.cate.index',['data' => $data,'request' => $request->all()]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //分类添加表单
        //DB::raw里面可以写原生sql语句
        //concat 连接字串或数组
        $data = DB::table('cate')
        ->select('*',DB::raw("CONCAT(path,',',id) AS sortPath"))
        ->orderby('sortPath')
        ->get();

        foreach($data as $k=>$v)
        {   //获取path里','的个数
            //substr_repeat重复一个字符串
            $num = substr_count($v ->path,',');

            //利用','的个数去区分分类名
            $data[$k] -> name = str_repeat('|----', $num).$v ->name;
        }


        return view('admin.cate.add',['data' =>$data]);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //执行分类添加
        // 思路
        //根据获取的pid的值去获取父分类的paht
        //再将父分类的paht.pid就是这个分类的path
        $data = $request -> except('_token');
        
        if($data['pid'] =='0')
        {
            $data['path'] ='0';
        }else
        {
           $parentPath = DB::table('cate') -> where('id',$data['pid']) ->first()
        ->path;
        $data['path'] = $parentPath.','.$data['pid']; 
        }

        
        //执行添加
        $res = DB::table('cate') ->insert($data);

        if($res)
        {
            return redirect('/admin/cate') -> with(['info' => '添加成功']);
        }else
        {
            return back() ->with(['info' => '添加失败']);
        }
         
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $data = DB::table('cate') ->where('id',$id) ->first(); //查出一条分类的数据
        $allData = DB::table('cate') //查出所有分类数据
        ->select('*',DB::raw("CONCAT(path,',',id) AS sortPath"))
        ->orderby('sortPath')
        ->get();

        foreach($allData as $k=>$v)
        {   //获取path里','的个数
            //substr_repeat重复一个字符串
            $num = substr_count($v ->path,',');

            //利用','的个数去区分分类名
            $allData[$k] -> name = str_repeat('|----', $num).$v ->name; //给所有分类加符号区分,就形成了父分类
        }
        return view('admin.cate.edit',['data' => $data,'allData' => $allData]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
         $data = $request -> except('_token','_method');
         
          if($data['pid'] =='0')
        {
            $data['path'] ='0';
        }else
        {
           $parentPath = DB::table('cate') -> where('id',$data['pid']) ->first()
        ->path;
        $data['path'] = $parentPath.','.$data['pid']; 
        }

        $parentPath = DB::table('cate') -> where('id',$data['pid']) ->first()
        ->path;
        $data['path'] = $parentPath.','.$data['pid'];
        //执行添加
        $res = DB::table('cate') ->where('id',$id) ->update($data);
        if($res)
        {
            return redirect('/admin/cate') -> with(['info' => '修改成功']);
        }else
        {
            return back() ->with(['info' => '修改失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
       $r=DB::table('cate') -> where('pid',$id) ->first();
       if($r)
       {
        return back() -> with(['info' =>'有子分类,不能删除']);
       }
        $res = DB::table('cate') ->delete($id);
         if($res)
        {
            return redirect('/admin/cate') -> with(['info' => '删除成功']);
        }else
        {
            return back() ->with(['info' => '删除失败']);
        }
    }
        
}
